//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QDate>
#include <QHash>
#include <QSqlRecord>
#include "CPontComptableFrm.h"
#include "CCalendrierFrm.h"
#include "CPrintFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CPontComptableFrm::CPontComptableFrm(QString name, QWidget *parent) : CMyMainWindow(name, parent) {
	setupUi(this);
	
	masterTable="pontComptable";
	primaryKey="id";
	
	loadPrintPopupMenu(toolBar);
	
	createGrille();
	
	chargeDonnees();
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CPontComptableFrm::setActionCur(CGlobal::EAction action) {
	actionCur=action;
	
	switch(actionCur) {
		case CGlobal::acNone:
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			enableBtnPrint(true);
			break;
		case CGlobal::acAffiche:
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			enableBtnPrint(false);
			break;
		case CGlobal::acAjout:
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			enableBtnPrint(false);
			break;
		default:
			break;
	}
}
//------------------------------------------------------------------------------
CGlobal::EAction CPontComptableFrm::getActionCur(void) {
	return actionCur;
}
//------------------------------------------------------------------------------
void CPontComptableFrm::closeEvent(QCloseEvent * event) {
	if(getActionCur() != CGlobal::acNone) {
		QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
							tr("Veuillez annuler ou valider avant de sortir"), QMessageBox::Ok);
							msgBox.exec();	
							
							event->ignore();
	}
}
//------------------------------------------------------------------------------
void CPontComptableFrm::chargeDonnees(void) {
	QString requete;
	QSqlQuery *query;
	CGlobal::EAction oldAction=getActionCur();
	
	setActionCur(CGlobal::acAffiche);
	
	requete="SELECT id, factureJusquAu, creation, createur, dateSupression, destructeur, suprimer ";
	requete+="FROM pontComptable ORDER BY id DESC";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		listPont->loadGrille(query);
		
		delete query;
	}
	listPont->resizeColumnsToContents();
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CPontComptableFrm::createGrille(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	CColumnModel *cm;
	
	columnModel->append(new CColumnModelInt(cpnId, "id", tr("Identifiant"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cpnFactureJusquAu, "factureJusquAu", tr("Sélection des factures jusqu'au"), Qt::AlignLeft, new QIcon(":coMexpert/images/1day.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cpnCreation, "creation", tr("Date de creation"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cpnCreateur, "createur", tr("Par"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	cm=new CColumnModelDate(cpnSupression, "dateSupression", tr("Date de supression"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled);
	columnModel->append(cm);
	cm->setDefaultValue("");
	columnModel->append(new CColumnModelString(cpnDestructeur, "destructeur", tr("Par"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	
	listPont->setColumnsModel(columnModel);
	listPont->setTable("pontComptable");
	listPont->setChamp("id");
}
//------------------------------------------------------------------------------
void CPontComptableFrm::on_actAjouter_triggered(void) {	
	int col;
	
	setActionCur(CGlobal::acAjout);
	
	listPont->getColumnModel(cpnId)->setDefaultValue("*");
	listPont->getColumnModel(cpnCreateur)->setDefaultValue(CMySQL::getInstance()->getUser());
	
	listPont->insertRow(0);
	
	for(col=0;col<listPont->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel *columnModel=listPont->getColumnModel(col);
		
		item=new CMyTableWidgetItem(columnModel->getDefaultValue(), columnModel->getType());
		item->setTextAlignment(columnModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columnModel->getIcon());
		item->setFlags(columnModel->getFlags());
		
		listPont->setItem(0, col, item);
	}
	
	listPont->resizeRowsToContents();
	on_listPont_cellDoubleClicked(0, cpnFactureJusquAu);
}
//------------------------------------------------------------------------------
void CPontComptableFrm::on_actValider_triggered(void) {
	QDate factureJusquAu=QDate::fromString(listPont->item(0, cpnFactureJusquAu)->text(), "dd/MM/yyyy");
	QVariant numPont;
	CPontComptable *pontComptable;

	QString requete="INSERT INTO pontComptable ";
	requete+="(factureJusquAu, creation, createur) ";
	requete+="SELECT \""+factureJusquAu.toString("yyyy-MM-dd")+"\", ";
	requete+="CURDATE(), \""+CMySQL::getInstance()->getUser()+"\" ";
	
	CMySQL::getInstance()->execQuery(requete, true, &numPont);
	
	requete="UPDATE teteFactures SET idPont="+numPont.toString()+" WHERE idPont IS NULL AND creation <= \""+factureJusquAu.toString("yyyy-MM-dd")+"\"";
	
	CMySQL::getInstance()->execQuery(requete);
	
	pontComptable=new CPontComptable(numPont.toInt());
	
	pontComptable->create();
	pontComptable->save();
	
	delete pontComptable;
	
	chargeDonnees();
	listPont->setValue(numPont.toString());
	
	print(btnPrint->defaultAction()->data().value<CDAODocToPrintEx *>());
	
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CPontComptableFrm::on_actAnnuler_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir annuler ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		chargeDonnees();
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CPontComptableFrm::on_actSupprimer_triggered(void) {
	int numPont=listPont->item(listPont->currentRow(), cpnId)->text().toInt();
	
	if(listPont->item(listPont->currentRow(), cpnDestructeur)->text() == "") {
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir annuler le pont n° ")+QString::number(numPont), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete="UPDATE pontComptable SET ";
			requete+="suprimer=\"OUI\", dateSupression=CURDATE(), destructeur=\""+CMySQL::getInstance()->getUser()+"\" ";
			requete+="WHERE id="+QString::number(numPont);
		
			CMySQL::getInstance()->execQuery(requete);
			
			requete="UPDATE teteFactures SET idPont=NULL WHERE idPont="+QString::number(numPont);
		
			CMySQL::getInstance()->execQuery(requete);
			
			chargeDonnees();
			setActionCur(CGlobal::acNone);
		}
	}
}
//------------------------------------------------------------------------------
void CPontComptableFrm::on_listPont_beforeInsert(const QSqlQuery *query, CMyTableWidgetItem *item) {
	QSqlRecord rec=query->record();
	
	if((rec.value("suprimer").toString() == "OUI")) {
		item->setBackground(QBrush(QColor(255, 0, 0)));
		item->setForeground(QBrush(QColor(255, 255, 255)));
	}
}
//------------------------------------------------------------------------------
void CPontComptableFrm::on_listPont_cellDoubleClicked(int row, int column) {
	if(row == 0 && column == cpnFactureJusquAu && getActionCur() == CGlobal::acAjout) {
		QTableWidgetItem *item=listPont->item(row, column);
		CCalendrierFrm *calendrierFrm=new CCalendrierFrm(this);
				
		calendrierFrm->setDate(QDate::fromString(item->text(), "dd/MM/yyyy"));
		calendrierFrm->setWindowTitle(tr("Sélection des factures jusqu'au"));
		calendrierFrm->exec();
		
		if(calendrierFrm->result() == QDialog::Accepted) {
			item->setText(calendrierFrm->getDate().toString("dd/MM/yyyy"));
		}
		
		delete calendrierFrm;
	}
}
//------------------------------------------------------------------------------
void CPontComptableFrm::on_listPont_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn) {
	QTableWidgetItem *item=listPont->item(currentRow, cpnId);
	
	if(item) {
		listPont->setValue(item->text());
	}
}
//------------------------------------------------------------------------------